package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsCouponMapper;
import com.shop.mapper.DtsCouponUserMapper;
import com.shop.pojo.DtsCategory;
import com.shop.pojo.DtsCoupon;
import com.shop.pojo.DtsCouponUser;
import com.shop.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsCouponServiceImpl
 * @Author xiao宋
 * @Date 8:17  2022/10/20
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsCouponServiceImpl implements DtsCouponService {
    @Autowired
    DtsCouponMapper couponMapper;
    @Autowired
    DtsCouponUserMapper userMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, Integer status, Integer type) {
        QueryWrapper<DtsCoupon> w = new QueryWrapper<>();
        IPage<DtsCoupon> iPage = new Page<>(page, limit);
        if (type != null) {
            w.eq("type", type);
        }
        if (status != null) {
            w.eq("status", status);
        }
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        w.eq("deleted", false);
        couponMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public Map<String, Object> findByCouponUserPage(Integer page, Integer limit, String sort, String order, Integer status, Integer userId, Integer couponId) {
        QueryWrapper<DtsCouponUser> w = new QueryWrapper<>();
        IPage<DtsCouponUser> iPage = new Page<>(page, limit);
        if (userId != null) {
            w.eq("user_id", userId);
        }
        if (couponId != null) {
            w.eq("coupon_id", couponId);
        }
        if (status != null) {
            w.eq("status", status);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        w.eq("deleted", false);
        userMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public DtsCoupon findByCouponId(Integer id) {
        QueryWrapper<DtsCoupon>w=new QueryWrapper<>();
        w.eq("id",id);
        DtsCoupon dtsCoupon = couponMapper.selectOne(w);
        return dtsCoupon;
    }


}
